﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace mOceanWindows
{
    internal partial class frmPopup : Form
    {
        public const int LWA_ALPHA = 0x2;
        public const int LWA_COLORKEY = 0x1;
        public enum RICH_MEDIA_TYPE
        {
	        DELAY = 0,
	        CLICK = 1
        };

        public class RICH_MEDIA
        {
	        public RICH_MEDIA()
	        {
		        nType = RICH_MEDIA_TYPE.DELAY;
		        nDelay = nWidth = nHeight = nOffsetX = nOffsetY = nRepeat = 0;
	        }
	        public RICH_MEDIA_TYPE nType;
	        public int nDelay;
	        public int colorTransparent;
	        public bool bFullscreen;
	        public int nWidth;
	        public int nHeight;
	        public int nOffsetX;
	        public int nOffsetY;
	        public int nRepeat;
	        public String strUrl;
        };

        internal RICH_MEDIA _rich;
        internal String _strFile;
        internal IntPtr _hwndParent;
        internal Rectangle _rcParent;
        internal Rectangle _rcApp;
        internal Timer _timer = new Timer();

        public frmPopup()
        {
            InitializeComponent();
        }

        internal void _timer_Tick(object sender, EventArgs e)
        {
            Rectangle rcClient = GetParentRect();

            if (rcClient != _rcParent)
            {
                _rcParent = rcClient;
                _rcApp = GetAppRect();

                Move();
            }
        }

        internal new void Move()
        {
	        Rectangle rcPopup;

	        if (_rich.bFullscreen)
	        {
		        rcPopup = _rcApp;
	        }
	        else{
		        rcPopup = new Rectangle( _rcParent.Left + _rich.nOffsetX, _rcParent.Top + _rich.nOffsetY,
                    _rich.nWidth, _rich.nHeight );
	        }
	        MoveWindow( Handle, rcPopup.Left, rcPopup.Top, rcPopup.Width,rcPopup.Height, false );
        }


        public void SetParent(IntPtr hwnd)
        {
            _hwndParent = hwnd;
            _rcParent = GetParentRect();
            _rcApp = GetAppRect();
            _timer.Interval = 30;
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Enabled = true;
        }

        public void ShowPopup()
        {
            axShockwaveFlash.Movie = _strFile;
        }

        internal Rectangle GetAppRect()
        {
            return AdViewControl.GetAppRect(_hwndParent);
        }


        internal Rectangle GetParentRect()
        {
            return AdViewControl.GetParentRect(_hwndParent);
        }

        protected override void OnShown(EventArgs e)
        {
            Visible = false;
            SetLayeredWindowAttributes(Handle, Convert.ToUInt32(_rich.colorTransparent), 0, LWA_COLORKEY);
            Move();
            ShowPopup();
            base.OnShown(e);

            axShockwaveFlash.Visible = false;
            Visible = true;
            axShockwaveFlash.Visible = true;
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= WS_EX_LAYERED;
                cp.Style |= unchecked((int)0x80000000);
                return cp;
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            _timer.Enabled = false;
        }

        private void axShockwaveFlash_FSCommand(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
        {
            if (String.Compare( e.command, "quit", true) == 0)
            {
                PostMessage( Handle, 0x10, IDOK, 0 );
            }

        }
        #region "import"
        [DllImport("user32.dll")]
        static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

        [DllImport("user32.dll")]
        static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);

        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        public static extern IntPtr GetParent(IntPtr hWnd);

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);

        [DllImport("user32.dll", SetLastError = true)]
        internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); 
        
        private const uint WM_COMMAND = 0x0111;
        private const uint IDOK = 0x01;
        private const Int32 GWL_ID = -12;

        private const Int32 GWL_STYLE = -16;
        private const Int32 GWL_EXSTYLE = -20;

        // Window Styles
        private const UInt32 WS_OVERLAPPED = 0;
        private const UInt32 WS_POPUP = 0x80000000;
        private const UInt32 WS_CHILD = 0x40000000;
        private const UInt32 WS_MINIMIZE = 0x20000000;
        private const UInt32 WS_VISIBLE = 0x10000000;
        private const UInt32 WS_DISABLED = 0x8000000;
        private const UInt32 WS_CLIPSIBLINGS = 0x4000000;
        private const UInt32 WS_CLIPCHILDREN = 0x2000000;
        private const UInt32 WS_MAXIMIZE = 0x1000000;
        private const UInt32 WS_CAPTION = 0xC00000;      // WS_BORDER or WS_DLGFRAME  
        private const UInt32 WS_BORDER = 0x800000;
        private const UInt32 WS_DLGFRAME = 0x400000;
        private const UInt32 WS_VSCROLL = 0x200000;
        private const UInt32 WS_HSCROLL = 0x100000;
        private const UInt32 WS_SYSMENU = 0x80000;
        private const UInt32 WS_THICKFRAME = 0x40000;
        private const UInt32 WS_GROUP = 0x20000;
        private const UInt32 WS_TABSTOP = 0x10000;
        private const UInt32 WS_MINIMIZEBOX = 0x20000;
        private const UInt32 WS_MAXIMIZEBOX = 0x10000;
        private const UInt32 WS_TILED = WS_OVERLAPPED;
        private const UInt32 WS_ICONIC = WS_MINIMIZE;
        private const UInt32 WS_SIZEBOX = WS_THICKFRAME;

        // Extended Window Styles
        private const UInt32 WS_EX_DLGMODALFRAME = 0x0001;
        private const UInt32 WS_EX_NOPARENTNOTIFY = 0x0004;
        private const UInt32 WS_EX_TOPMOST = 0x0008;
        private const UInt32 WS_EX_ACCEPTFILES = 0x0010;
        private const UInt32 WS_EX_TRANSPARENT = 0x0020;
        private const UInt32 WS_EX_MDICHILD = 0x0040;
        private const UInt32 WS_EX_TOOLWINDOW = 0x0080;
        private const UInt32 WS_EX_WINDOWEDGE = 0x0100;
        private const UInt32 WS_EX_CLIENTEDGE = 0x0200;
        private const UInt32 WS_EX_CONTEXTHELP = 0x0400;
        private const UInt32 WS_EX_RIGHT = 0x1000;
        private const UInt32 WS_EX_LEFT = 0x0000;
        private const UInt32 WS_EX_RTLREADING = 0x2000;
        private const UInt32 WS_EX_LTRREADING = 0x0000;
        private const UInt32 WS_EX_LEFTSCROLLBAR = 0x4000;
        private const UInt32 WS_EX_RIGHTSCROLLBAR = 0x0000;
        private const UInt32 WS_EX_CONTROLPARENT = 0x10000;
        private const UInt32 WS_EX_STATICEDGE = 0x20000;
        private const UInt32 WS_EX_APPWINDOW = 0x40000;
        private const UInt32 WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
        private const UInt32 WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
        private const Int32 WS_EX_LAYERED = 0x00080000;
        private const UInt32 WS_EX_NOINHERITLAYOUT = 0x00100000; // Disable inheritence of mirroring by children
        private const UInt32 WS_EX_LAYOUTRTL = 0x00400000; // Right to left mirroring
        private const UInt32 WS_EX_COMPOSITED = 0x02000000;
        private const UInt32 WS_EX_NOACTIVATE = 0x08000000;

        #endregion
    }
}
